package com.xinke.module.system.service.user;

import cn.hutool.core.collection.CollUtil;
import com.xinke.component.common.enums.CommonStatusEnum;
import com.xinke.component.common.util.sno.IdGen;
import com.xinke.module.system.model.entity.user.DeptDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.PostDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.user.PostMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.component.common.util.collection.CollectionUtils.convertMap;
import static com.xinke.component.common.util.collection.CollectionUtils.convertSet;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 岗位信息 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class PostServiceImpl implements PostService {

    @Resource
    private PostMapper postMapper;
    @Resource
    private DeptService deptService;
    @Override
    public Long createPost(PostSaveReqVO createReqVO) {
        // 插入
        PostDO post = BeanUtils.toBean(createReqVO, PostDO.class);
        if(Objects.isNull(post.getPostId())){
            post.setPostId(IdGen.nextId());
        }
        postMapper.insert(post);
        // 返回
        return post.getPostId();
    }

    @Override
    public void updatePost(PostSaveReqVO updateReqVO) {
        // 校验存在
        validatePostExists(updateReqVO.getPostId());
        // 更新
        PostDO updateObj = BeanUtils.toBean(updateReqVO, PostDO.class);
        postMapper.updateById(updateObj);
    }

    @Override
    public void deletePost(Long postId) {
        // 校验存在
        validatePostExists(postId);
        // 删除
        postMapper.deleteById(postId);
    }

    private void validatePostExists(Long postId) {
        if (postMapper.selectById(postId) == null) {
            throw exception(null);
        }
    }

    @Override
    public PostDO getPost(Long postId) {
        return postMapper.selectById(postId);
    }

    @Override
    public PageResult<PostDO> getPostPage(PostPageReqVO pageReqVO) {
        return postMapper.selectPage(pageReqVO,getDeptCondition(pageReqVO.getDeptId()));
    }

    @Override
    public void validatePostList(Collection<Long> postIds) {
        if (CollUtil.isEmpty(postIds)) {
            return;
        }
        // 获得岗位信息
        List<PostDO> posts = postMapper.selectBatchIds(postIds);
        Map<Long, PostDO> postMap = convertMap(posts, PostDO::getPostId);
        // 校验
        postIds.forEach(id -> {
            PostDO post = postMap.get(id);
            if (post == null) {
                throw exception(POST_NOT_FOUND);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(post.getPostStatus())) {
                throw exception(POST_NOT_ENABLE, post.getPostName());
            }

        });
    }
    @Override
    public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
        return postMapper.selectList(ids, statuses);
    }
    /**
     * 获得部门条件：查询指定部门的子部门编号们，包括自身
     *
     * @param deptId 部门编号
     * @return 部门编号集合
     */
    private Set<Long> getDeptCondition(Long deptId) {
        if (deptId == null) {
            return Collections.emptySet();
        }
        Set<Long> deptIds = convertSet(deptService.getChildDeptList(deptId), DeptDO::getDeptId);
        deptIds.add(deptId); // 包括自身
        return deptIds;
    }
}